package com.hospital.demo.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Repository;

import java.util.Random;

@Repository
public class MD5Util {

    public static final String SALT = "dbee0908005afa28";
    /**
     * 加盐加密
     * @param password
     * @return
     */
    public String inputPassFromPass(String password){
        String str=""+SALT.charAt(0)+SALT.charAt(2)+password+SALT.charAt(6)+SALT.charAt(4);
        return md5(str);
    }

    /**
     *
     * @param formPass:输入的密码
     * @param salt：加的盐
     * @return
     */
    public String formPassToDBPass(String formPass, String salt) {
        String str = ""+salt.charAt(0)+salt.charAt(2) + formPass +salt.charAt(6) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 加密
     * @param password
     * @return
     */
    public String md5(String password) { return DigestUtils.md5Hex(password); }

    public String getRandString(){
        int length = 16;
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

}
